package com.project.smarthome.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.project.smarthome.common.util.UIUtils;

/**
 * Created by jie on 2017/10/19.
 */

public class InterLinearLayout extends LinearLayout {

    private OnTouchMove mOnTouchMove;
    private boolean mCanMove;

    public void setmOnTouchMove(OnTouchMove mOnTouchMove) {
        this.mOnTouchMove = mOnTouchMove;
        setFocusable(true);
    }

    public InterLinearLayout(Context context) {
        super(context);
        setFocusable(true);
    }

    public InterLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
    }

    public InterLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFocusable(true);
    }

    float startX;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX(0);
                break;
            case MotionEvent.ACTION_MOVE:
                if (!mCanMove) {
                    return false;
                } else {
                    if ((event.getX(0) - startX) > (UIUtils.getScreenWidth() / 10) || (startX - event.getX(0)) > (UIUtils.getScreenWidth() / 10)) {
                        if (event.getX(0) > startX) {
                            if (mOnTouchMove != null) {
                                mOnTouchMove.onMove(false);
                            }
                            return true;
                        } else {
                            if (mOnTouchMove != null) {
                                mOnTouchMove.onMove(true);
                            }
                            return true;
                        }
                    }
                    return false;
                }
        }
        return super.onInterceptHoverEvent(event);

    }

    public void setmCanMove(boolean mCanMove) {
        this.mCanMove = mCanMove;
    }

    public interface OnTouchMove {
        void onMove(boolean isRight);
    }
}
